<template>
  <div class="layout">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="header-content">
        <div class="logo-section">
          <i class="ri-archive-line"></i>
          <h1>数字档案管理系统</h1>
        </div>
        
        <nav class="nav-menu">
          <router-link to="/" class="nav-item" :class="{ active: $route.path === '/' }">
            首页
          </router-link>
          <router-link to="/archive-receive" class="nav-item" :class="{ active: $route.path === '/archive-receive' }">
            档案接收
          </router-link>
          <router-link to="/archive-manage" class="nav-item" :class="{ active: $route.path === '/archive-manage' }">
            档案管理
          </router-link>
          <router-link to="/archive-search" class="nav-item" :class="{ active: $route.path === '/archive-search' }">
            智能检索
          </router-link>
          <router-link to="/statistics" class="nav-item" :class="{ active: $route.path === '/statistics' }">
            数据统计
          </router-link>
        </nav>
        
        <div class="user-section">
          <div class="notification">
            <i class="ri-notification-3-line"></i>
            <span class="badge">3</span>
          </div>
          <div class="user-info" @click="showUserMenu = !showUserMenu">
            <img :src="userAvatar" alt="用户头像" class="avatar">
            <span class="username">{{ currentUser.realName || '管理员' }}</span>
          </div>
          
          <!-- 用户菜单 -->
          <div v-if="showUserMenu" class="user-menu">
            <a href="#" @click="handleUserCommand('profile')">个人资料</a>
            <a href="#" @click="handleUserCommand('settings')">系统设置</a>
            <hr>
            <a href="#" @click="handleUserCommand('logout')">退出登录</a>
          </div>
        </div>
      </div>
    </header>

    <!-- 主内容区 -->
    <main class="main-content">
      <router-view />
    </main>
  </div>
</template>

<script>
import { mapState, mapActions } from 'vuex'

export default {
  name: 'Layout',
  data() {
    return {
      showUserMenu: false,
      userAvatar: 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?ixlib=rb-1.2.1&auto=format&fit=crop&w=100&h=100&q=80'
    }
  },
  computed: {
    ...mapState(['user']),
    currentUser() {
      return this.user || { realName: '管理员' }
    }
  },
  mounted() {
    // 点击外部关闭用户菜单
    document.addEventListener('click', (e) => {
      if (!this.$el.contains(e.target)) {
        this.showUserMenu = false
      }
    })
  },
  methods: {
    ...mapActions(['logout']),
    handleUserCommand(command) {
      this.showUserMenu = false
      switch (command) {
        case 'profile':
          console.log('个人资料功能开发中')
          break
        case 'settings':
          console.log('系统设置功能开发中')
          break
        case 'logout':
          this.handleLogout()
          break
      }
    },
    async handleLogout() {
      try {
        await this.logout()
        this.$router.push('/login')
        console.log('退出登录成功')
      } catch (error) {
        console.error('退出登录失败', error)
      }
    }
  }
}
</script>

<style scoped>
.layout {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
}

.header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  box-shadow: 0 2px 4px rgba(0,0,0,0.1);
  position: relative;
  z-index: 1000;
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 20px;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.logo-section {
  display: flex;
  align-items: center;
  gap: 8px;
}

.logo-section i {
  font-size: 24px;
}

.logo-section h1 {
  font-size: 20px;
  font-weight: 600;
  margin: 0;
}

.nav-menu {
  display: flex;
  gap: 30px;
}

.nav-item {
  color: white;
  text-decoration: none;
  padding: 8px 16px;
  border-radius: 4px;
  transition: all 0.3s ease;
  font-size: 14px;
}

.nav-item:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.nav-item.active {
  background-color: rgba(255, 255, 255, 0.2);
  font-weight: 500;
}

.user-section {
  display: flex;
  align-items: center;
  gap: 20px;
  position: relative;
}

.notification {
  position: relative;
  cursor: pointer;
}

.notification i {
  font-size: 20px;
}

.badge {
  position: absolute;
  top: -8px;
  right: -8px;
  background-color: #ff4757;
  color: white;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 10px;
  min-width: 16px;
  text-align: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
  padding: 4px 8px;
  border-radius: 4px;
  transition: background-color 0.3s ease;
}

.user-info:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  object-fit: cover;
}

.username {
  font-size: 14px;
}

.user-menu {
  position: absolute;
  top: 100%;
  right: 0;
  background: white;
  border-radius: 8px;
  box-shadow: 0 4px 12px rgba(0,0,0,0.15);
  padding: 8px 0;
  min-width: 120px;
  z-index: 1001;
}

.user-menu a {
  display: block;
  padding: 8px 16px;
  color: #333;
  text-decoration: none;
  font-size: 14px;
  transition: background-color 0.3s ease;
}

.user-menu a:hover {
  background-color: #f5f5f5;
}

.user-menu hr {
  margin: 4px 0;
  border: none;
  border-top: 1px solid #eee;
}

.main-content {
  flex: 1;
  padding: 20px;
  background-color: #f8f9fa;
  min-height: calc(100vh - 60px);
}

/* 响应式设计 */
@media (max-width: 768px) {
  .header-content {
    padding: 0 15px;
  }
  
  .nav-menu {
    display: none;
  }
  
  .username {
    display: none;
  }
}
</style>